package com.uvnos4j.matilda.commons.verification;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 验证码Servlet
 * <p>
 * Description:验证码Servlet
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @since 2020-08-21 22:52:37
 */
public class VerificationCodeServlet extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = 7871012845149848068L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 随机生成字符串，并写入session
        String code = Verification.getRandCode(4);
        req.getSession().setAttribute("VerificationCode", code);

        BufferedImage image = Verification.getImage(60, 22, code);
        resp.setContentType("image/png");
        OutputStream out = resp.getOutputStream();
        out.write(Verification.getByteArray(image));
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

}